import { ajax } from '../lib/ajax.js';
import { tools } from '../lib/tools.js';

let txt_tname = document.getElementById('txt_tname');
let txt_league = document.getElementById('txt_league');
let txt_champions = document.getElementById('txt_champions');
let txt_cups = document.getElementById('txt_cups');
let txt_goals = document.getElementById('txt_goals');
let txt_gname = document.getElementById('txt_gname');
let txt_fans = document.getElementById('txt_fans');
let txt_created = document.getElementById('txt_created');
let txt_intro = document.getElementById('txt_intro');
let btnOk = document.getElementById('btnOk');
let btnBack = document.getElementById('btnBack');

let ftid = location.search.replace('?ftid=', '');

ajax.get(`/crud/footballteam/${ftid}`, {}, (data) => {
  if (!data.success) {
    alert(data.message);
    return;
  }
  let intro = data.data;
  txt_tname.value = intro.tname;
  txt_league.value = intro.league;
  txt_champions.value = intro.champions;
  txt_cups.value = intro.cups;
  txt_goals.value = intro.goals;
  txt_gname.value = intro.gname;
  txt_fans.value = intro.fans;
  txt_created.value = tools.formatDate(intro.created, 'yyyy-MM-dd');
  txt_intro.value = intro.intro;
});

btnOk.addEventListener('click', () => {
  let modi = {
    ftid:ftid,
    tname:txt_tname.value,
    league:txt_league.value,
    intro:txt_intro.value,
    champions:txt_champions.value,
    cups:txt_cups.value,
    fans:txt_fans.value,
    goals:txt_goals.value,
    gname:txt_gname.value,
    created:txt_created.value,
  };
  ajax.put('/crud/footballteam', modi, (data) => {    
    alert(data.message);
  });
});

btnBack.addEventListener('click', () => {
  location.href = 'fbt.html';
});
